﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace com.lover.common
{
    public class UiUtils
    {
        private static UiUtils _instance = new UiUtils();
        public static UiUtils getInstance() { return _instance; }
        private UiUtils() { }

        private int _lastX = -1;
        private int _lastY = -1;
        private bool _dragging = false;
        public void registerMovableForm(Form win)
        {
            win.MouseDown += win_MouseDown;
            win.MouseMove += win_MouseMove;
            win.MouseUp += win_MouseUp;

            for (int i = 0, len = win.Controls.Count; i < len; i++)
            {
                Control control = win.Controls[i];
                control.MouseDown += win_MouseDown;
                control.MouseMove += win_MouseMove;
                control.MouseUp += win_MouseUp;
            }
        }

        private void win_MouseUp(object sender, MouseEventArgs e)
        {
            _dragging = false;
        }

        private void win_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_dragging) return;
            Form win;
            Rectangle rcScr;
            Rectangle rcWin = new Rectangle(e.X, e.Y, 1, 1);
            if (sender is Form)
            {
                win = sender as Form;
                rcScr = win.RectangleToScreen(rcWin);
            }
            else
            {
                Control ctrl = sender as Control;
                rcScr = ctrl.RectangleToScreen(rcWin);
                win = ctrl.Parent as Form;
                if (win == null) return;
            }
            
            int winTargetX = win.Location.X + (rcScr.X - _lastX), winTargetY = win.Location.Y + (rcScr.Y - _lastY);
            _lastX = rcScr.X; _lastY = rcScr.Y;

            win.SetDesktopLocation(winTargetX, winTargetY);
        }

        private void win_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            Form win;
            Rectangle rcScr;
            Rectangle rcWin = new Rectangle(e.X, e.Y, 1, 1);
            if (sender is Form)
            {
                win = sender as Form;
                rcScr = win.RectangleToScreen(rcWin);
            }
            else
            {
                Control ctrl = sender as Control;
                rcScr = ctrl.RectangleToScreen(rcWin);
                win = ctrl.Parent as Form;
                if (win == null) return;
            }
            _lastX = rcScr.X; _lastY = rcScr.Y;

            _dragging = true;
        }

        public void info(string msg)
        {
            MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void error(string msg)
        {
            MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public DialogResult confirm(string msg)
        {
            return MessageBox.Show(msg, "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }
    }
}
